//
//  LoginViewController.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/12.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "LoginViewControllerBall.h"
#import "Predicate.h"

@interface LoginViewControllerBall ()
@property(nonatomic, strong)UIButton *cancelButton;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIButton *areaCodeButton;
@property(nonatomic, strong)UIView *line1;
@property(nonatomic, strong)UIView *line2;
@property(nonatomic, strong)UIView *line3;
@property(nonatomic, strong)UIView *line4;
@property(nonatomic, strong)UITextField *phoneTextFiled;
@property(nonatomic, strong)UITextField *verifiyTextFiled;
///获取验证码
@property(nonatomic, strong)UIButton *securityButton;
///登录按钮
@property(nonatomic, strong)UIButton *loginButton;
@end

@implementation LoginViewControllerBall

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"filter_delete"] style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonClick)];

    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"登录";
    self.titleLabel.textColor = [UIColor xm_jetblackColor];
    self.titleLabel.font = [UIFont xm_medium:30.0];
    [self.view addSubview:self.titleLabel];
    
    self.line1 = [self line];
    self.areaCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.areaCodeButton setTitle:@"+86" forState:UIControlStateNormal];
    [self.view addSubview:self.areaCodeButton];
    
    self.areaCodeButton.backgroundColor = XMRandomColor;
    
    self.line3 = [self line];
    self.phoneTextFiled = [[UITextField alloc] init];
    self.phoneTextFiled.placeholder = @"请输入用户名";
    [self.view addSubview:self.phoneTextFiled];
    
    self.line2 = [self line];
    self.line4 = [self line];
    self.verifiyTextFiled = [[UITextField alloc] init];
    self.verifiyTextFiled.placeholder = @"请输入验证码";
    [self.view addSubview:self.verifiyTextFiled];
    
    self.securityButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.securityButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.securityButton.titleLabel.font = [UIFont xm_font:14.0];
    [self.securityButton setTitleColor:[UIColor xm_middleColor] forState:UIControlStateNormal];
    [self.securityButton addTarget:self action:@selector(securityButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.securityButton];
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginButton.backgroundColor = [UIColor xm_somberColor];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
//    self.loginButton.enabled = NO;
    self.loginButton.clipsToBounds = YES;
    self.loginButton.layer.cornerRadius = 22.0;
    [self.loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginButton];

    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(20);
    }];
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(15);
        make.right.mas_equalTo(self.view).offset(-15);
        make.height.mas_equalTo(@0.8);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(80);
    }];
    
    [self.areaCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 40));
        make.left.mas_equalTo(self.line1);
        make.bottom.mas_equalTo(self.line1.mas_top).offset(-10);
    }];
    [self.line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.areaCodeButton);
        make.size.mas_equalTo(CGSizeMake(0.8, 20));
        make.left.mas_equalTo(self.areaCodeButton.mas_right).offset(6);
    }];
    
    [self.phoneTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.line3.mas_right).offset(6);
        make.centerY.mas_equalTo(self.line3);
        make.right.mas_equalTo(self.line1);
        make.height.mas_equalTo(@30);
    }];
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(15);
        make.right.mas_equalTo(self.view).offset(-15);
        make.height.mas_equalTo(@0.8);
        make.top.mas_equalTo(self.line1.mas_bottom).offset(80);
    }];
    [self.securityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.right.mas_equalTo(self.line2);
        make.bottom.mas_equalTo(self.line2.mas_top).offset(-20);
    }];
    [self.line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(0.8, 20));
        make.right.mas_equalTo(self.securityButton.mas_left).offset(-6);
        make.centerY.mas_equalTo(self.securityButton);
    }];
    [self.verifiyTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.line2);
        make.bottom.mas_equalTo(self.line2.mas_top).offset(-20);
        make.right.mas_equalTo(self.line4.mas_left).offset(-6);
        make.height.mas_equalTo(@30);
    }];

    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.line2);
        make.top.mas_equalTo(self.line2.mas_bottom).offset(50);
        make.height.mas_equalTo(@44);
    }];
}

-(UIView *)line{
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = [UIColor xm_splitlineColor];
    [self.view addSubview:view];
    return view;
}

-(void)cancelButtonClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)securityButtonClick{
    if (![Predicate isValidPhoneNum:self.phoneTextFiled.text]) {
        [MBProgressHUD showError:@"请输入正确的手机号"];
        return;
    }
    NSDictionary * dict = @{@"Phone":self.phoneTextFiled.text,@"TickeType":@(1)};
    [FBNetworkTool get:@"userapi/com.SendSmsCode" parameters:dict showHUD:YES success:^(XMHttpResponseModel *responseModel) {
        if (responseModel.code == 200) {
            [self.securityButton xm_startTime:60 title:@"获取验证码" waitTittle:@"后获取"];
        }
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(void)loginButtonClick{
    if (![Predicate isValidPhoneNum:self.phoneTextFiled.text]) {
        [MBProgressHUD showError:@"请输入正确的手机号"];
        return;
    }
    if (self.verifiyTextFiled.text.length == 0) {
        [MBProgressHUD showError:@"请输入验证码"];
        return;
    }
    NSDictionary * dict = @{@"Phone":self.phoneTextFiled.text,
                            @"SMSAuthenticationCode":self.verifiyTextFiled.text
                            };
    [FBNetworkTool get:@"userapi/com.Register" parameters:dict showHUD:YES success:^(XMHttpResponseModel *responseModel) {
        if (responseModel.code == 200) {
            UserAccount * account = [UserAccount mj_objectWithKeyValues:responseModel.data];
            account.isLogin = YES;
            [UserAccountTool saveAccount:account];
            
            [self cancelButtonClick];
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
